<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */		
	defined ('IN_AGORA') or die;

	class TaskController extends AgoraAdminController {
		function loadModel() {
			$this->model = Model::getInstance('BanModel');
		}
		function _default() {
			$this->view->template = 'configuration/bans/list';

			$this->setPagination($this->model->loadAllCount());
			$bans = $this->model->loadAll('',$this->pagination->per_page,$this->pagination->page);

			$this->view->assignRef('bans',$bans);
		}

		function add_ip() {
			$this->view->template = 'configuration/bans/add_ip';
		}

		function add_user() {
			$this->view->template = 'configuration/bans/add_user';

		}

		function cancel() {
			$this->redirect();
		}

		function edit()
		{
			$cid = Agora::getPostVar('cid');
			$id = array_shift($cid);

			$ban = $this->model->load($id);
			$ban['expire'] = getdate($ban['expire'] - AGORA_TIME - intval(date('Z')));

			$this->view->assignRef('ban',$ban);
			$ban['ip'] = explode('.', $ban['ip']);
			$this->view->template = 'configuration/bans/edit_user';
		}

		function save() {
			$form = Agora::getPostVar('form');
			$user_model = & Model::getInstance('UserModel');
			
			if ($form['ip']) {
				// Ban by IP
				array_map('intval', $form['ip']);
				$form['ip'] = implode('.', $form['ip']);
			}
            if ($form['username']) {
				if (!$user_model->exists($form['username'],'username')) {
					Agora::showError('Username not found');
					$this->view->template = 'configuration/bans/add_user';
					return;
				}
			}
			if ($form['email']) {
				if (!$user_model->exists($form['email'],'email')) {
					Agora::showError('Email not found');
					$this->view->template = 'configuration/bans/add_user';
					return;
				}
			    
			}

			$e = & $form['expire'];
			$expire = intval($e['days'])*3600 * 24 + intval($e['hours']) * 3600 + intval($e['minutes']) * 60;
			$form['expire'] = AGORA_TIME + $expire;

			if (isset($form['id'])) {
				$this->model->edit($form['id'],$form);
			} else {
				$this->model->add($form);
			}
			
			$real_email = $user_model->getUserEmail($form['username']);
			$message = $form['message'] ? $form['message'] : JText::_('BAN_EMAIL_NOREASON');
			$mail = JFactory::getMailer();
			$mail->addRecipient($real_email);
			$mail->setSubject(JText::_('BAN_EMAIL_SUBJECT'));
			$mail->setBody(JText::sprintf('BAN_EMAIL_BODY', $message));
			$mail->IsHTML(true);
			$mail->Send();
			
			$this->redirect();
		}

		function remove() {
			$cid = Agora::getPostVar('cid');
			$db = & JFactory::getDBO();
			$query = 'SELECT b.id, u.email FROM jos_agora_bans b JOIN jos_agora_users au ON b.username = au.username JOIN jos_users u ON au.jos_id = u.id WHERE b.id IN (' . implode(',',$cid) . ')';
			$db->setQuery($query);
			$result = $db->loadAssocList('id');
			foreach ($cid as $id) {
				$this->model->delete($id);
				$mail = JFactory::getMailer();
				$mail->addRecipient($result[$id]['email']);
				$mail->setSubject(JText::_('UNBAN_EMAIL_SUBJECT'));
				$mail->setBody(JText::_('UNBAN_EMAIL_BODY'));
				$mail->IsHTML(true);
				$mail->Send();
			}
			$this->redirect();
		}
	}

